<?php

class cIndex extends ControllerBase{

	protected $defaultaction = 'index';
	protected $template = "public/index.html";	
  
  	protected $module = 'public';
  
	protected $route = array(
								
								  );

	public function init(){
		global $smarty;
		
		if ( isset($_SESSION['login']) ){
			include('app/controllers/init.php');
		}
		//$smarty->assign("content","_blank.html");
	}

	public function rights(){
		//si mando un action diferente se ejecuta el else, pero como por defecto el action es index
		//sale del rigths y ejecuta el indexAction.
		if ( $this->action == 'login' ) return true;
		if ( $this->action == 'logout' ) return true;
		if ( $this->action == 'index' ) return true;  
		if ( $this->action == 'ie' ) return true;
	
		if ( ! isset($_SESSION['login']) ){
			$this->redirect("public");
			die("NO LOGIN");	
			return false;
		}	else {
			$this->redirect('admin');
		}
		
		return true;
	}

	public function dashboardAction(){
		global $smarty;
		
		
	}
	
	public function indexAction(){
		
		if ( isset($_SESSION['login']) ){
				$usuario = $_SESSION['login'];
				$this->redirect('admin');				
		}
		else{		
			$this->redirect("public","login");
		}
			
	}
	
	public function loginAction(){
		global $smarty;
			//$smarty->assign("contador", time());
			if ( isset($_REQUEST['username']) ){
				//$response = 0;
				$usuario = false;
				//ORMConnection::debug(true);
				try{
					//llama a la funcion login del modelo usuario
					$usuario = Usuario::login(@$_REQUEST['username'],@$_REQUEST['userkey']); 
				}catch(Exception $e){
							
					//$smarty->assign("contador", $_REQUEST['time']);
					$smarty->assign("fail", "Las credenciales ingresadas son Incorrectos");
					
					//$smarty->assign("fail", "Usuario o Contraseña no validos");
				}			
				
				if ( $usuario  !=false  ){
					
					$usuario = $_SESSION['login'];
					$this->redirect('admin');
										
					 return true;
				}
	
			$smarty->assign("content","public/login.html");
		}
	}
	
	public function logoutAction(){
		unset($_SESSION['login']);
		$this->redirect("public", "login");
	}
	
	 
	 	
}

?>
